//
//  SendMessageVC.m
//  phmall
//
//  Created by admin on 2017/12/19.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "SendMessageVC.h"
#import "EvaluateText.h"
#import "FriendsListViewController.h"
#import "FriendsModel.h"
@interface SendMessageVC ()<UITableViewDelegate,UITableViewDataSource,FriendsListDelegate>
@property(strong,nonatomic)UITableView *myTableview;
@property(strong,nonatomic)UIButton *name;
@property(strong,nonatomic)UITextField *caption;
@property(strong,nonatomic)EvaluateText *message;
@end

@implementation SendMessageVC
-(void)ChooseFriend:(NSMutableArray *)list{
    NSMutableArray *nameList=[[NSMutableArray alloc]init];
    for (FriendsModel *model in list) {
        [nameList addObject:model.member_id];
    }
    [self.name setTitle:[nameList componentsJoinedByString:@","] forState:UIControlStateNormal];
    [self.name layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:0];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarBgAlpha=@"1.0";
    //    // 改变NavBar顶层View的颜色
    self.navigationController.navigationBar.barTintColor =  [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=Localized(@"消息");

    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    
    
    [self.view addSubview:self.myTableview];
    [self.myTableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(0).priorityHigh();
    }];
    
    UIView *headerView=[UIView new];
    headerView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    UIView *nameView=[[UIView alloc]init];
    nameView.backgroundColor=[UIColor whiteColor];
    [headerView addSubview:nameView];
    [nameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.left.right.offset(0);
        make.height.offset(44);
    }];
    
    
    UILabel *nameLabel=[[UILabel alloc]init];
    [nameView addSubview:nameLabel];
    nameLabel.text=[NSString stringWithFormat:@"%@:",Localized(@"收货人")];
    nameLabel.font=[UIFont systemFontOfSize:15];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(10);
    }];
    CGSize nameSize = [nameLabel.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    self.name=[[UIButton alloc]init];
    [nameView addSubview:self.name];
//    [self.name setTitle:@"你猜你猜猜你猜" forState:UIControlStateNormal];
    [self.name setImage:[UIImage imageNamed:@"前往"] forState:UIControlStateNormal];
    self.name.titleLabel.font=[UIFont systemFontOfSize:15];
    self.name.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    if (!self.ID) {
        [self.name addTarget:self action:@selector(friendsAction) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.name setTitleColor:[UIColor colorWithHexString:@"#adadad"] forState:UIControlStateNormal];
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.width.offset(SCREEN_WIDTH-nameSize.width-30);
        make.centerY.equalTo(nameView);
    }];
    

    UIView *titleView=[[UIView alloc]init];
    titleView.backgroundColor=[UIColor whiteColor];
    [headerView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameView.mas_bottom).offset(1);
        make.left.right.offset(0);
        make.height.offset(44);
    }];
    
    UILabel *title=[[UILabel alloc]init];
    title.text=[NSString stringWithFormat:@"%@:",Localized(@"标题")];
    title.font=[UIFont systemFontOfSize:15];
    [titleView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.centerY.equalTo(titleView);
    }];
    CGSize titleSize = [title.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    self.caption = [[UITextField alloc]init];
    [titleView addSubview:self.caption];
    self.caption.placeholder = Localized(@"标题");
    self.caption.font=[UIFont systemFontOfSize:14];
    [self.caption mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.offset(SCREEN_WIDTH-titleSize.width-30);
        make.left.offset(titleSize.width+20);
    }];

    UIView *messageView=[[UIView alloc]init];
    messageView.backgroundColor=[UIColor whiteColor];
    [headerView addSubview:messageView];
    [messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView.mas_bottom).offset(1);
        make.left.right.offset(0);
        make.height.offset(100);
        make.bottom.equalTo(headerView.mas_bottom).with.offset(-0).priorityHigh();
    }];
    
    UILabel *message=[[UILabel alloc]init];
    [messageView addSubview:message];
    message.text=[NSString stringWithFormat:@"%@:",Localized(@"消息")];
    message.font=[UIFont systemFontOfSize:15];
    [message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(11);
        make.left.offset(10);
        //        make.right.equalTo(self.street.mas_left).offset(-10);
    }];
    
    CGSize messageSize = [message.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;

    self.message=[[EvaluateText alloc]init];
    [messageView addSubview:self.message];
    self.message.placeholder=Localized(@"内容");;
    self.message.font=[UIFont systemFontOfSize:14];
    [self.message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(4);
        make.bottom.offset(-4);
        make.width.offset(SCREEN_WIDTH-messageSize.width-20);
        make.left.offset(messageSize.width+15);
    }];
    //
 
    CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = headerView.bounds;
    frame.size.height = height;
    headerView.frame = frame;
    [self.myTableview setTableHeaderView:headerView];
    
    UIButton* send=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:send];
    [send setTitle:@"发送" forState:UIControlStateNormal];
    [send addTarget:self action:@selector(refreshSend) forControlEvents:UIControlEventTouchUpInside];
    [send setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    send.backgroundColor=[UIColor orangeColor];
    //    [logOut setBackgroundImage:[UIImage imageNamed:@"背景渐变条"] forState:UIControlStateNormal];
    [send mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(49);
        make.bottom.equalTo(self.view).offset(-self.tabBar_height);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view.mas_right);
    }];
    // Do any additional setup after loading the view.
}
-(void)friendsAction{
    self.hidesBottomBarWhenPushed=YES;
    FriendsListViewController *evaluate=[[FriendsListViewController alloc]init];
    evaluate.delegate=self;
    [self.navigationController pushViewController:evaluate animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"newCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;;
    }
    cell.opaque=YES;
    cell.layer.drawsAsynchronously=YES;
    cell.layer.rasterizationScale=[UIScreen mainScreen].scale;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //分割线补全
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//MARK:-GETTER
- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        _myTableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _myTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableview.showsVerticalScrollIndicator=NO;
        //        _myTableview.emptyDataSetSource=self;
        //        _myTableview.emptyDataSetDelegate=self;
        _myTableview.scrollEnabled = YES;
    }
    return _myTableview;
}

- (void)refreshSend{
    
    NSString *urlstr =  [NSString stringWithFormat:@"%@%@",HomeURL,@"sms_send/send"];
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    [parametersDic setObject:self.name.titleLabel.text forKey:@"to_id"];
    [parametersDic setObject:self.caption.text forKey:@"title"];
    [parametersDic setObject:self.message.text forKey:@"content"];
    [parametersDic setObject:@"0" forKey:@"is_broadcast"];
    
    [httpTool ZBPostNetDataWith:urlstr withDic:parametersDic andSuccess:^(id responseObject) {
        //        [self.messageArray removeAllObjects];
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([[NSString stringWithFormat:@"%@",responseDict[@"ret"]] isEqualToString:@"200"]) {
            if ([[NSString stringWithFormat:@"%@",responseDict[@"data"]] isEqualToString:@"1"]) {
                NSLog(@"发送成功");
            }
            [self.myTableview reloadData];
            //            [self.hud hideAnimated:YES];
        }
    } andFailure:^{
        //        [self.hud hideAnimated:YES];
    }];
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
